package za.org.jcicada.comms.impl.stub;

import java.io.IOException;

import za.org.jcicada.comms.IMessageListener;
import za.org.jcicada.comms.impl.ConnectionBase;
import za.org.jcicada.comms.messages.CommsMessage;

/** Null implementation of the IConnection interface.
 */
public class StubConnection extends ConnectionBase {

	private boolean closed;
	
    /**
     * Creates and starts the handler thread.
     */
    @Override
    public void initialize(IMessageListener listener) throws IOException {
        super.initialize(listener);
        closed = false;
    }

    /**
     * Closes this connection and the connection it is attached to.
     */
    public void close() throws IOException {
        try {
        	closed = true;
            getListener().onClose();
        } catch (Exception ex) {
            throw new IOException(ex);
        }
    }
    
    public boolean isClosed() {
    	return closed;
    }

    public void sendMessage(CommsMessage message) throws IOException {
        try {
            getListener().onMessage(message);
        } catch (Exception ex) {
            throw new IOException(ex);
        }
    }
    
}
